﻿using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace SellAvi.Model
{
    public partial class AvitoMailingItem : AbstractModel
    {
        public int AvitoUser_Id { get; set; }
        public virtual AvitoUser AvitoUser { get; set; }
        public string SendToUrl { get; set; }
        public string MessageText { get; set; }
        public int MessageStackId { get; set; }
        public DateTime? DatePosted { get; set; }
        public string ChatRoomId { get; set; }

        public int AdId {
            get
            {
                var productIdRegex = new Regex(@"\d+$");
                var productId = productIdRegex.Match(SendToUrl).Value;
                return int.Parse(productId);
            }
        }
        public string LatinProductName
        {
            get
            {
                Uri tempValue;
                char[] charsToTrim = { '_','0','1','2','3','4','5','6','7','8','9'};
                if (Uri.TryCreate(SendToUrl, UriKind.Absolute, out tempValue))
                {
                    //new Uri("https://www.avito.ru/").MakeRelativeUri(tempValue).ToString();
                    var segments = tempValue.Segments;
                    return segments.LastOrDefault().TrimEnd(charsToTrim);
                }
                return String.Empty;
            }
        }
    }
}
